home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 605 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.4 KB  |  86 lines

  1. Path: news.mira.net.au!news
  2. From: davidw@werple.net.au (David White)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Creating a pointer to a function "void (*ptrFunction)()" inside a class
  5. Date: 5 Jan 1996 21:39:03 +1100
  6. Organization: Werple Internet, Melbourne
  7. Message-ID: <4civ47$hg6@werple.net.au>
  8. References: <30ECA10F.3D99@ifu.net>
  9. NNTP-Posting-Host: werple.mira.net.au
  10.  
  11. Jason Gresh <gresh@ifu.net> writes:
  12.  
  13. >Hi,
  14.  
  15. >    I am trying to create a base class that has a function that the 
  16. >derived class needs to create.  In order to do this, I want to create a 
  17. >pointer to a function in the base class that the derived class can then 
  18. >define.  This is not a case where an override will work because the 
  19. >number and names of the derived functions is not known.  Hopefully this 
  20. >code sample will make this clearer:
  21.  
  22. >class BaseClass
  23. >{
  24. >    int (*ptrFunction)();
  25. >}
  26.  
  27. >class DerivedClass : BaseClass
  28. >{
  29. >    DerivedClass::DerivedClass();
  30. >    int myFunction(int, int);
  31. >}
  32.  
  33. >DerivedClass::DerivedClass()
  34. >{
  35. >    ptrFunction = myFunction;
  36. >}
  37.  
  38. >DerivedClass::myFunction(int, int)
  39. >{
  40. >// code ....
  41. >}
  42.  
  43. >If I don't make myFunction part of the derived class (global), 
  44. >everything works fine.  As soon as I include it in the class, I cannot 
  45. >assign a pointer to it.  I am aware that pointers to functions inside 
  46. >the class include the class name in some way ( this is not an issue for 
  47. >global functions).  What is the casting (or other) mechanism to make
  48. >this work?
  49.  
  50.  
  51. Non-static member functions are fundamentally different from global
  52. functions; they receive an implicit parameter (the 'this' pointer),
  53. through which they can access the data of the object instance for which
  54. they are called. Consequently, a pointer to such a function is
  55. incompatible with a global function pointer. Use the following syntax
  56. instead: 
  57.  
  58.     class BaseClass
  59.     {
  60.     protected:
  61.         // A typedef simplifies things somewhat. 
  62.     // Also, the correct function parameters (int, int) are required
  63.         typedef int (BaseClass::*FPTR)(int, int);
  64.         FPTR ptrFunction;
  65.     };
  66.  
  67.     class DerivedClass : BaseClass
  68.     {
  69.     public:
  70.         DerivedClass()
  71.     { ptrFunction = (FPTR)&DerivedClass::myFunction; 
  72.     }
  73.     int myFunction(int, int);
  74.     }
  75.  
  76. The (FPTR) cast is required because 'myFunction' is not a member of
  77. BaseClass.  Here is an example of calling the function: 
  78.  
  79.     void BaseClass::f()
  80.     { int n = (this->*ptrFunction)(1, 2); 
  81.     }
  82.  
  83. David White
  84. davidw@werple.mira.net.au
  85.  
  86.